package secondriver.aboxlibrary.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;

import secondriver.aboxlibrary.R;


/**
 * PassWordView
 * <p/>
 * leftComponent是一个EditText
 * RightComponent是一个CheckBox
 * <p/>
 * Author : secondriver
 * Created : 2015/11/25
 */
public class PassWordView extends LinearLayout {

    private EditText mPassWordEditText;
    private CheckBox mShowCheckBox;

    private LayoutParams mPassWordParams;
    private LayoutParams mShowParams;

    private float leftComponentWeight;
    private float rightComponentWeight;
    private int inputType;

    public PassWordView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initProperty(context, attrs);
        initComponent(context);
    }

    private final void initProperty(Context context, AttributeSet attrs) {
        //获取设置的值,未设置使用默认值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PassWordView);
        leftComponentWeight = typedArray.getFloat(R.styleable.PassWordView_left_component_weight, 5.0f);
        rightComponentWeight = typedArray.getFloat(R.styleable.PassWordView_right_component_weight, 1.0f);
        //来自Android系统已定义的属性
        inputType = typedArray.getInt(R.styleable.PassWordView_android_inputType, InputType.TYPE_TEXT_VARIATION_PASSWORD);
        typedArray.recycle();
    }

    private void initComponent(Context context) {
        mPassWordEditText = new EditText(context);
        mPassWordEditText.setInputType(inputType);

        mPassWordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());

        mShowCheckBox = new CheckBox(context);

        //通过权重来决定EditText和CheckBox占据父视图的空间的比例
        mPassWordParams = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, leftComponentWeight);
        mShowParams = new LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, rightComponentWeight);

        //父视图是一个容器视图，指定水平排列子视图
        setOrientation(HORIZONTAL);
        addView(mPassWordEditText, mPassWordParams);
        addView(mShowCheckBox, mShowParams);

        addCheckBoxListener();
    }

    /**
     * 获取PassWord
     *
     * @return
     */
    public String getPassWord() {
        return mPassWordEditText.getText().toString();
    }

    /**
     * 获取PassWord组件
     *
     * @return
     */
    public EditText getPassWordEditText() {
        return mPassWordEditText;
    }

    private final void addCheckBoxListener() {

        /**
         * CheckBox点击事件处理
         *
         * 如果选中EditText中的密码明文显示
         *
         * 如果未选中EditText中的密码黑点显示
         *
         */
        mShowCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mPassWordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    mPassWordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
    }
}
